/**
 * 
 */
package com.mc.hutool;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;

import cn.hutool.core.lang.Console;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * 生成测试数据
 * 
 * @author w.weizhen
 *
 */
public class ProduceTestData {
	public static final String data_path = System.getProperty("user.dir") + File.separator + "data" + File.separator + "hutool" + File.separator;//数据目录
	private static String text = null;
	private static List<String> texts = null;
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		try {
			text = FileUtils.readFileToString(new File(data_path + "text.txt"), "UTF-8");
			texts = Arrays.asList(text.split("[\\p{IsPunctuation}\\p{IsWhite_Space}]+"));
			System.out.println();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		CronUtil.schedule("*/3 * * * * ?", new Task() {
			@Override
			public void execute() {
				Random random = new Random();
				int opt = random.nextInt(10);
				if (Arrays.asList(0,1,2,3,4,5,6).contains(opt)) 
				{
					add();
				}
				else if(Arrays.asList(7,8).contains(opt))
				{
					edit();
				}
				else if(opt == 9)
				{
					del();
				}
			}
		});

		// 支持秒级别定时任务
		CronUtil.setMatchSecond(true);
		CronUtil.start();
	}
	
	
	private static void add()
	{
		Log log = LogFactory.get();
		List<String> sqls = Arrays.asList(
			"insert into sys_dept values(%s,%s,'%s','%s',%s,'%s','%s','%s','%s','%s','%s',sysdate(),'',null);",
			"insert into sys_user values(%s,%s,'%s','%s','%s','%s','%s','%s','','%s','%s','%s','%s',sysdate(),'%s',sysdate(),'',null,'%s');"
		);
		try {
			Random random = new Random();
			String sql = sqls.get(random.nextInt(sqls.size()));
			if(sql.indexOf(" sys_dept ") > -1)
			{
				sql = String.format(
					sql,
					random.nextInt(10000000),
					random.nextInt(10000),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(10000),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(1),
					random.nextInt(1),
					texts.get(random.nextInt(texts.size()))
				);
			}
			else if(sql.indexOf(" sys_user ") > -1)
			{
				sql = String.format(
					sql, 
					random.nextInt(10000000),
					random.nextInt(10000),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(10),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(1),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(1),
					random.nextInt(1),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size()))
					);
			}
			
			Db.use().execute(sql);
		} catch (SQLException e) {
			log.error(e, "SQL error!");
		}
	}
	
	private static void edit()
	{
		Log log = LogFactory.get();
		List<String> idsqls = Arrays.asList(
			"SELECT dept_id AS id FROM sys_dept ORDER BY RAND() LIMIT 1;",
			"SELECT user_id AS id FROM sys_user ORDER BY RAND() LIMIT 1;"
			);
		List<String> upsqls = Arrays.asList(
			"UPDATE sys_dept SET parent_id = %s,ancestors = '%s',dept_name = '%s',order_num = %s,leader = '%s',phone = '%s',email = '%s',status = '%s',del_flag = '%s',create_by = '%s',create_time = sysdate(),update_by = '',update_time = null WHERE dept_id = %s;;",
			"UPDATE sys_user SET dept_id=%s,user_name='%s',nick_name='%s',user_type='%s',email='%s',phonenumber='%s',sex='%s',avatar='',password='%s',status='%s',del_flag='%s',login_ip='%s',login_date=sysdate(),create_by='%s',create_time=sysdate(),update_by='',update_time=null,remark='%s' WHERE user_id=%s;"
		);
		try {
			Random random = new Random();
			String sql = idsqls.get(random.nextInt(idsqls.size()));
			
			List<Entity> result = Db.use().query(sql);
			if (result == null || result.size() < 1) {return;}
			Entity entity = result.get(0);
			String id = entity.getStr("id");
			if(sql.indexOf(" sys_dept ") > -1)
			{
				sql = upsqls.get(0);
				sql = String.format(
					sql,
					random.nextInt(10000),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(10000),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(1),
					random.nextInt(1),
					texts.get(random.nextInt(texts.size())),
					id
				);
			}
			else if(sql.indexOf(" sys_user ") > -1)
			{
				sql = upsqls.get(1);
				sql = String.format(
					sql, 
					random.nextInt(10000),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(10),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(1),
					texts.get(random.nextInt(texts.size())),
					random.nextInt(1),
					random.nextInt(1),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					texts.get(random.nextInt(texts.size())),
					id
					);
			}
			
			Db.use().execute(sql);
		} catch (SQLException e) {
			log.error(e, "SQL error!");
		}
	}
	
	private static void del()
	{
		Log log = LogFactory.get();
		List<String> idsqls = Arrays.asList(
			"SELECT dept_id AS id FROM sys_dept ORDER BY RAND() LIMIT 1;",
			"SELECT user_id AS id FROM sys_user ORDER BY RAND() LIMIT 1;"
			);
		List<String> desqls = Arrays.asList(
			"DELETE FROM sys_dept WHERE dept_id = %s;",
			"DELETE FROM sys_user WHERE user_id = %s;"
		);
		try {
			Random random = new Random();
			String sql = idsqls.get(random.nextInt(idsqls.size()));
			
			List<Entity> result = Db.use().query(sql);
			if (result == null || result.size() < 1) {return;}
			Entity entity = result.get(0);
			String id = entity.getStr("id");
			if(sql.indexOf(" sys_dept ") > -1)
			{
				sql = desqls.get(0);
				sql = String.format(
					sql,
					id
				);
			}
			else if(sql.indexOf(" sys_user ") > -1)
			{
				sql = desqls.get(1);
				sql = String.format(
					sql, 
					id
					);
			}
			
			Db.use().execute(sql);
		} catch (SQLException e) {
			log.error(e, "SQL error!");
		}
	}
	
}